/**
 * 
 */
package com.demo2do.strawberry.entity.startup.fund;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.springframework.format.annotation.DateTimeFormat;

import com.alibaba.fastjson.annotation.JSONField;
import com.demo2do.core.utils.DateUtils;
import com.demo2do.strawberry.entity.startup.Startup;

/**
 * @author lute
 */
@Entity
@Table(name = "startup_fund")
public class Fund {
	
	@Id
	@GeneratedValue(generator = "gen")
	@GenericGenerator(name = "gen", strategy = "foreign", parameters = @Parameter(name = "property", value = "startup"))
	@JSONField(serialize = false)
	private Long id;
	
	@Type(type = "date")
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date startDate;
	
	@Type(type = "date")
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date endDate;
	
	/** 融资金额 */
	private BigDecimal total;
	
	/** 出让股份比例 */
	private BigDecimal share;
	
	/** 最小投资份额 */
	private BigDecimal minimum;
	
	/** 已获投资总额 */
	private BigDecimal fundedSum;
	
	/** 当前是否被领投  */
	@Column(name = "is_leaded")
	private boolean leaded;
	
	@OneToOne(fetch = FetchType.LAZY)
	@PrimaryKeyJoinColumn
	@JSONField(serialize = false)
	private Startup startup;
	
	/**
	 * default constructors
	 */
	public Fund() {
		
	}
	
	/**
	 * @param startup
	 */
	public Fund(Startup startup) {
		this.startup = startup;
		this.fundedSum = BigDecimal.valueOf(0);
		this.leaded = false;
	}
	
	/**
	 * return the percentage of funded money
	 * 
	 * @return
	 */
	public BigDecimal getFundedPercentage() {
		if(total == null || total.equals(BigDecimal.ZERO)) {
			return BigDecimal.ZERO;
		}
		return fundedSum.divide(total, 2, RoundingMode.HALF_UP);
	}
	
	/**
	 * return the remaining days of fundings
	 * 
	 * @return
	 */
	public int getRemainingDays() {
		if(this.endDate == null) {
			return 0;
		}
		return DateUtils.compareTwoDatesOnDay(this.endDate, new Date());
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @return the startDate
	 */
	public Date getStartDate() {
		return startDate;
	}

	/**
	 * @return the endDate
	 */
	public Date getEndDate() {
		return endDate;
	}

	/**
	 * @return the total
	 */
	public BigDecimal getTotal() {
		return total;
	}

	/**
	 * @return the share
	 */
	public BigDecimal getShare() {
		return share;
	}

	/**
	 * @return the minimum
	 */
	public BigDecimal getMinimum() {
		return minimum;
	}

	/**
	 * @return the fundedSum
	 */
	public BigDecimal getFundedSum() {
		return fundedSum;
	}

	/**
	 * @return the leaded
	 */
	public boolean isLeaded() {
		return leaded;
	}

	/**
	 * @return the startup
	 */
	public Startup getStartup() {
		return startup;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	/**
	 * @param total the total to set
	 */
	public void setTotal(BigDecimal total) {
		this.total = total;
	}

	/**
	 * @param share the share to set
	 */
	public void setShare(BigDecimal share) {
		this.share = share;
	}

	/**
	 * @param mimum the minimum to set
	 */
	public void setMinimum(BigDecimal minimum) {
		this.minimum = minimum;
	}

	/**
	 * @param fundedSum the fundedSum to set
	 */
	public void setFundedSum(BigDecimal fundedSum) {
		this.fundedSum = fundedSum;
	}

	/**
	 * @param leaded the leaded to set
	 */
	public void setLeaded(boolean leaded) {
		this.leaded = leaded;
	}

	/**
	 * @param startup the startup to set
	 */
	public void setStartup(Startup startup) {
		this.startup = startup;
	}

}
